home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CFMExamples / ModApp / ModuleSources / GWorldTools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  4.1 KB  |  199 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        GWorldTools.c
  3.  
  4.     Contains:    A group of handy utilities for managing off-screen graphics worlds
  5.  
  6.     Written by:    Richard Clark
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.                  2/16/94    RC        Changed NewGWorld to allocate purgeable buffers
  13.                  1/27/94    RC        Added pervasive error checking (i.e. is the buffer not NIL?)
  14.                                      before performing any operation.
  15.                  1/14/94    RC        Created
  16.  
  17.     To Do:
  18. */
  19.  
  20.  
  21. #ifndef __QUICKDRAW__
  22.     #include <Types.h>
  23. #endif
  24.  
  25. #ifndef __QUICKDRAW__
  26.     #include <Quickdraw.h>
  27. #endif
  28.  
  29. #ifndef __QUICKDRAW__
  30.     #include <Windows.h>
  31. #endif
  32.  
  33. #ifndef __QDOFFSCREEN__
  34.     #include <QDOffscreen.h>
  35. #endif
  36.  
  37. #ifndef __ERRORS__
  38.     #include <Errors.h>
  39. #endif
  40.  
  41. #ifndef __MEMORY__
  42.     #include <Memory.h>
  43. #endif
  44.  
  45. #include "GWorldTools.h"
  46.  
  47. Rect GetGlobalBounds (WindowPtr wp)
  48. {
  49.     /* return the bounds of this window, in global coordinates         */
  50.     Rect  bounds;
  51.  
  52.     if (((WindowPeek)wp)->visible)
  53.         bounds = (*((WindowPeek)wp)->contRgn)->rgnBBox;
  54.     else
  55.         /* get the size in local coordinates since we can't depend on anything else */
  56.         bounds = wp->portRect;
  57.  
  58.     return bounds;
  59. }
  60.  
  61. OSErr LockBuffer (GWorldPtr buffer, GWorldFlags *oldPixState)
  62. {
  63.     PixMapHandle    bufferPixMap;
  64.     OSErr            err = noErr;
  65.     
  66.     if (buffer) {
  67.         /* Lock down our off-screen image */
  68.         bufferPixMap = GetGWorldPixMap(buffer);
  69.         *oldPixState = GetPixelsState(bufferPixMap);
  70.         if (!LockPixels(bufferPixMap))
  71.             err = kPixelsPurged;
  72.     }
  73.     return err;
  74. }
  75.  
  76.  
  77. OSErr UnlockBuffer (GWorldPtr buffer, GWorldFlags oldPixState)
  78. {
  79.     PixMapHandle    bufferPixMap;
  80.  
  81.     /* Undo the effects of "LockBuffer" */
  82.     if (buffer) {
  83.         bufferPixMap = GetGWorldPixMap(buffer);
  84.         SetPixelsState(bufferPixMap, oldPixState);
  85.     }
  86.     return noErr;
  87. }
  88.  
  89. OSErr CopyBufferToWindow (WindowPtr wp, GWorldPtr buffer)
  90. {
  91.     /* Copy the image from the offscreen buffer to the window */
  92.     OSErr            err;
  93.     RgnHandle        imageRgn, growIconRgn;
  94.     Rect            portRect, growIconRect;
  95.     GWorldFlags        oldPixState;
  96.     PixMapHandle    bufferPixMap;
  97.  
  98.     portRect = wp->portRect;
  99.     if (buffer) {
  100.         bufferPixMap = GetGWorldPixMap(buffer);
  101.         err = LockBuffer(buffer, &oldPixState);
  102.         if (err) goto done;
  103.     }
  104.     
  105.     /* Construct a mask region which omits the grow icon */
  106.     growIconRect = portRect;
  107.     growIconRect.top = growIconRect.bottom - 15;
  108.     growIconRect.left = growIconRect.right - 15;
  109.     imageRgn = NewRgn();
  110.     RectRgn(imageRgn, &portRect);
  111.     growIconRgn = NewRgn();
  112.     RectRgn(growIconRgn, &growIconRect);
  113.     DiffRgn(imageRgn, growIconRgn, imageRgn);
  114.     
  115.     /* Draw, podnah */
  116.     SetPort(wp);
  117.     ForeColor(blackColor);
  118.     BackColor(whiteColor);
  119.     CopyBits((BitMap*)*bufferPixMap,
  120.             &wp->portBits,
  121.             &buffer->portRect,
  122.             &portRect,
  123.             srcCopy, imageRgn);
  124.     
  125.     DisposeRgn(imageRgn);
  126.     DisposeRgn(growIconRgn);
  127.     
  128.     UnlockBuffer(buffer, oldPixState);
  129.  
  130. done:
  131.     return err;
  132. }
  133.  
  134.  
  135. OSErr UpdateBuffer (WindowPtr wp, GWorldPtr* buffer)
  136. {
  137.     Rect            globalBounds;
  138.     GWorldFlags        newGWorldState;
  139.     OSErr            err = noErr;
  140.     
  141.     if (*buffer) {
  142.         globalBounds = GetGlobalBounds(wp);
  143.         newGWorldState = UpdateGWorld(buffer, 0, &globalBounds, NULL, NULL, 0);
  144.         // We may have encountered an error, so see if the returned result is
  145.         // one of the known error codes.
  146.         if ((newGWorldState == paramErr) || (newGWorldState == cDepthErr))
  147.             err = (OSErr)newGWorldState;
  148.     }
  149.     return err;
  150. }
  151.  
  152.  
  153. OSErr DisposeBuffer (GWorldPtr* buffer)
  154. {
  155.     if (*buffer)
  156.         DisposeGWorld(*buffer);
  157.     *buffer = NULL;
  158.     return MemError();
  159. }
  160.  
  161. OSErr AllocateBuffer (WindowPtr wp, Rect bounds, GWorldPtr* buffer, Boolean disposeOld)
  162. {
  163. #pragma unused (wp)
  164.     OSErr            err = noErr;
  165.     CGrafPtr        oldPort;
  166.     GDHandle        oldDevice;
  167.     
  168.     GetGWorld(&oldPort,&oldDevice);
  169.  
  170.     if (disposeOld && (buffer != NULL) && (*buffer != NULL))
  171.         DisposeGWorld(*buffer);
  172.     
  173.     err = NewGWorld(buffer, 0, &bounds, NULL, NULL, pixPurge);
  174.     
  175.     if (err) {
  176.         *buffer = NULL;
  177.         goto done;
  178.     }
  179.     
  180.     /* Erase the new buffer */
  181.     if ((buffer) && (*buffer)) {
  182.         OSErr        err2;
  183.         GWorldFlags oldPixState;
  184.         
  185.         err2 = LockBuffer (*buffer, &oldPixState);
  186.         if (err2 == noErr) {
  187.             SetGWorld(*buffer, NULL);
  188.             EraseRect(&(*buffer)->portRect);
  189.             UnlockBuffer(*buffer, oldPixState);
  190.         }
  191.     } else
  192.         err = memFullErr;
  193.     
  194. done:
  195.     SetGWorld(oldPort, oldDevice);
  196.     return err;
  197. }
  198.  
  199.